import $AppConfig from '../dev/app_config.js';
import alertUI from '../dev/vue/plugin/alert';
import loadingUI from '../dev/vue/plugin/loading';

import AlloyLever from 'alloylever';

AlloyLever.config({
    entry: "#invitation"          //请点击这个DOM元素6次召唤vConsole。//你可以通过AlloyLever.entry('#entry2')设置多个机关入 召唤神龙
})

if (window.Vue) {
    Vue.use($AppConfig, {});
    Vue.use(alertUI);
    Vue.use(loadingUI);

    var vm = new Vue({
        el: '#invitation',
        data: {
            code: '',
            inApp: Vue.$AppConfig.inApp,
            access_token: Vue.$AppConfig.getToken,
            dom: HTMLDivElement,
            animId: null
        },
        methods: {
            focus() {
                setTimeout(() => {
                    this.scrollTop(this.dom);
                }, 500);
            },
            scrollTop(dom) {
                clearInterval(this.animId);
                this.animId = setInterval(() => {
                    let height = dom.clientHeight;
                    let scrHeight = dom.scrollHeight;
                    let scrTop = dom.scrollTop;
                    if (scrTop + height >= scrHeight) {
                        clearInterval(this.animId);
                        // document.body.scrollTop = document.documentElement.scrollTop = scrHeight;
                    } else {
                        scrTop += 1;
                        dom.scrollTop = scrTop;
                    }
                }, 60 / 1000);
            },
            upServer() {
                // 提交邀请码到服务器
                if (!this.inApp) {
                    this.$loading.hide();
                    return this.$alert.text("请在APP内打开");
                }
                if (this.trim(this.code).length < 8) {
                    return this.$alert.text("请输入正确邀请码");
                }
                // ajax 提交
                let body = {access_token: this.access_token, invitationCode: this.code};

                this.$http.post(Vue.$AppConfig.INTERFACE_HOST + "/useInvitationCode", body, {emulateJSON: true})
                    .then(response => {
                            this.$loading.hide();
                            let body = response.body;
                            this.appCallback(body);
                            if (body.success) {
                                // 邀请成功，返回上一页
                                Vue.$AppConfig.appJSBridge('keyBack')
                            } else {
                                // 弹窗提示错误
                                this.$alert.text(body.text);
                            }
                        },
                        () => {
                            this.$loading.hide();
                            this.$alert.text("网络连接错误,请稍后再试");
                        })
            },
            trim(str) {
                return str.replace(/(^\s*)|(\s*$)/g, "");
            },
            appCallback(body) {
                let state = true;
                if (!body.success) {
                    state = false;
                }
                if (Vue.$AppConfig.appJSBridge('inviteFlag', state)) {
                } else {
                    console.log("邀请回调失败");
                }


                // if (Vue.$AppConfig.isIos) {
                //     try {
                //         window.webkit.messageHandlers.inviteFlag.postMessage(state);
                //     } catch (e) {
                //         console.log(e);
                //         this.$alert.text("没找到inviteFlag");
                //     }
                // } else {
                //     try {
                //         window.JsBridge.inviteFlag(state);
                //     } catch (e) {
                //         console.log(e);
                //         this.$alert.text("没找到inviteFlag");
                //     }
                // }

            }
        },
        mounted: function () {

            if (Vue.$AppConfig.isIos) {
                this.dom = document.body;
            } else {
                this.dom = this.$refs.inv;
            }

            window.addEventListener('resize', () => {
                this.scrollTop(this.dom);
            })

        }

    })

}